package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.utils.IDUtils;
import com.yzkj.cpjxc.business.framework.utils.JsonUtils;
import com.yzkj.cpjxc.business.domain.SysUser;
import com.yzkj.cpjxc.business.domain.Multimedia;
import com.yzkj.cpjxc.business.service.MultimediaService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件下载公用控制器
 */
@Controller
@RequestMapping("/fileDownload")
public class FileDownloadController extends BaseController {
	
	@Autowired
	private MultimediaService multimediaService;

	/**
	 * 日志记录器
	 */
	public Logger log = Logger.getLogger(UploadifyController.class);

	@RequestMapping(value = "/uploadFile")
	public String uploadFile(HttpServletResponse response, HttpServletRequest request) throws Exception {

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			// 上传文件名 
			MultipartFile file = entity.getValue();
			
			//FileType filetype = FileTypeUtils.getType(file.getBytes());

			log.info("上传文件名称：" + file.getOriginalFilename());
			// 上传的文件名
			String originalFilename = file.getOriginalFilename();
			// 文件格式(后缀)
			String suffix = StringUtils.substring(originalFilename, StringUtils.lastIndexOf(originalFilename, ".") + 1);
			log.info("上传文件格式：" + suffix);
			
			/*if(!"xls".equals(suffix) && !("xlsx").equals(suffix) && !("xlsm").equals(suffix)){
				super.ajax(response,"上传文件格式不正确！");
				break;
			}*/
			
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			Date now = new Date();
			
			jsonMap.put("isSucc", "true");
			//添加附件信息
			String uuid = IDUtils.genUUID();
			jsonMap.put("multimediaUuid", uuid);
			jsonMap.put("fileName", file.getOriginalFilename());
			
			Multimedia mulFile = new Multimedia();
			//设置文件唯一标识
			mulFile.setUuid(uuid);
			//创建时间
			mulFile.setCreateTime(now);
			//关联外键
			mulFile.setFkUuid(request.getParameter("fkUuid"));
			//新文件名称
			//mulFile.setNewFileName(newFileName);
			//文件原始名称
			mulFile.setOidFileName(file.getOriginalFilename());
			//设置文件类型（后缀名）
			mulFile.setContentType(suffix);
			mulFile.setFileSize(file.getSize());
			//文件二进制内容
			mulFile.setFileData(file.getBytes());
			//设置登录用户名
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.CURRENT_USER);
			if(user!=null){
				mulFile.setUserName(user.getUsername());
			}
			//设置文件数据状态（页面传入） 0:临时存储 1：长期存储
			mulFile.setDataState(request.getParameter("dataState"));
			//执行创建
			multimediaService.create(mulFile);

			JsonUtils.responseJson(response, jsonMap);
		}
		return null;
	}

	/**
	 * 前端定时调用，防止网络连接掉线卡住
	 */
	@RequestMapping(value = "/networkConnections")
	public void networkConnections(HttpServletResponse response, HttpServletRequest request) throws Exception {
		//System.out.println("。。。。。。。。。。。。每分钟调用一次。。。。。。。。。。。。。");
	}
}
